package com.itlin.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.itlin.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author 所谓伊人
 * @date 2022/3/22 - 10:56
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Autowired
    private BaseContext baseContext;


    @Override
    public void insertFill(MetaObject metaObject) {
      log.info("公共字段自动填充【insert】");
      metaObject.setValue("createTime", LocalDateTime.now());
      metaObject.setValue("updateTime", LocalDateTime.now());
      metaObject.setValue("createUser", baseContext.getThreadLocal());
      metaObject.setValue("updateUser", baseContext.getThreadLocal());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充【update】");
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", baseContext.getThreadLocal());
    }
}
